<template>
	<view>
		<scroll-view scroll-y="true" :style="'height:'+scrollH+'px;'" @scrolltolower="loadMore()">
		<template v-if="searchList.length === 0">
		<view class="p-2 font-md">搜索历史</view>
		<view class="flex flex-wrap">
			<view class="border rounded font mx-2 my-1 px-2" v-for="(item,index) in listHistory" :key="index" hover-class="bg-light" @click="tapClickHistory(item)">{{item}}</view>
		</view>
		</template>
		<template v-else>
	   <block v-for="(item,index) in searchList" :key="index">
	      <common-list :item="item" :index="index"></common-list>
	   	  <divider></divider>
	  </block>
	     <load-more :loadmore="loadmore"></load-more>
		 
        </template>
		</scroll-view>
	</view>
</template>

<script>
 
	import commonList from '@/components/common/common-list.vue';	
	import loadMore from '@/components/common/load-more.vue';
	import Time from '../../common/time.js'
	import { mapState } from 'vuex';
	export default {
		
		components:{
			commonList,loadMore
		},
		data() {
			return {
			  scrollH:500,
			 searchText:"",
			 loadmore:'上拉加载更多',
			 searchList:[],
			 beginTime:'',
			 endTime:'',
			 listHistory:["社团活动","自习","观影","运动","购物","其他","本周","上周","下周"],
			 pagnation:{
			 	pageNum:1,
			 	pageSize:10
			 },
			}
		},
		
		onNavigationBarSearchInputChanged(e){
			console.log(e)
			this.searchText = e.text;
		},
		onNavigationBarButtonTap(e){
			if(e.index == 0)
			{
				this.searchEvent({searchValue:this.searchText});
			}
		},
		computed:{
			...mapState({
				loginStatus:state=>state.loginStatus
			}),
		},
		onLoad() {
			uni.getSystemInfo({
				success: (res) => {
					this.scrollH = res.windowHeight;
				}
			})
		},
		methods: {
			 tapClickHistory(item){	
				 
				 if(item=="本周"){
					 
					 this.beginTime = Time.getMonday('s',0);
					 this.endTime = Time.getMonday('e',0);
					 this.searchText = ''
				 }else  if(item=="上周"){
					 this.beginTime = Time.getMonday('s',-1);
					 this.endTime = Time.getMonday('e',-1);
					  this.searchText =''
				 }else  if(item=="下周"){
					 this.beginTime = Time.getMonday('s',1);
					 this.endTime = Time.getMonday('e',1);
					  
				 }else{
					 this.beginTime = '',
					 this.endTime = '',
					  this.searchText = item;
				 }	
			  this.searchEvent({searchValue:this.searchText,...this.pagnation,beginTime:this.beginTime,endTime:this.endTime})	   
			 },
			 loadMore(){	
			 	if(this.loadmore !== "上拉加载更多") return;
			 	this.loadmore ="加载中..."
			 	this.pagnation.pageNum++
			 	this.searchEvent({searchValue:this.searchText,...this.pagnation,beginTime:this.beginTime,endTime:this.endTime});	  
			 },
			searchEvent(param){
			    uni.hideKeyboard();
				uni.showLoading({
					title: '加载中...',
					mask: false
				});
				if(this.loginStatus){
				this.$R.get("/meet/event/listToken",param).then((res)=>{
					if(res.code ==200){
					 uni.hideLoading()
					  this.searchList = this.pagnation.pageNum==1?res.rows:[...res.rows,...this.searchList],
					 this.loadmore = res.rows.length<10?'没有更多了':'上拉加载更多'
					}
				})
				}else{
					this.$R.getNoToken('/meet/event/list',data).then((res)=>{
					if(res.code ==200){
					 uni.hideLoading()
					  this.searchList = this.pagnation.pageNum==1?res.rows:[...res.rows,...this.searchList],
					 this.loadmore = res.rows.length<10?'没有更多了':'上拉加载更多'
					}
					})
				}
			}
			
		}
	}
</script>

<style>

</style>
